<?php
namespace app\api\logic;
use app\common\model\RechargeTemplate;
use app\common\model\ShopConfig;
use think\Db;

class RechargeLogic
{

    public static function getTemplate(){
        $list = Db::name('recharge_template')
                ->where(['delete_time'=>0])
                ->order('money asc')
                ->field('id,money,give_money,is_recommend')
                ->select();
        foreach ($list as &$item){
            $item['tips'] = '';
            if($item['give_money'] > 0){
                $item['tips'] = '充'.intval($item['money']).'赠送'.intval($item['give_money']).'元';
            }
        }
        return $list;
    }

     public static function recharge($user_id,$post){
        //选择运费模板
        if($post['template_id']){
            $template = Db::name('recharge_template')
                    ->where(['delete_time'=>0,'id'=>$post['template_id']])
                    ->field('id,money,give_money')
                    ->find();
            $money = $template['money'];
            $give_money = $template['give_money'];
        }else{//自定义充值金额
            $template = Db::name('recharge_template')
                    ->where(['delete_time'=>0,'money'=>$post['money']])
                    ->field('id,money,give_money')
                    ->find();
            $money = $post['money'];
            $give_money = 0;
            if($template){
                $money = $template['money'];
                $give_money = $template['give_money'];
            }

        }
        $add_order = [
            'user_id'       => $user_id,
            'order_sn'         => 'C'.createSn('recharge_order', 'order_sn', '', 4),
            'order_amount'  => $money,
            'pay_status'    => 0,    //待支付状态；
            'pay_way'       => $post['pay_way'] ?? 1,
            'template_id'   => $template['id'] ?? 0,
            'give_money'    => $give_money,
            'create_time'   => time(),
            'update_time'   => time()
        ];
        // 启动事务
        Db::startTrans();
        try {
            $recharge_order_info=Db::name('recharge_order')->where(array('user_id'=>$user_id,'pay_status'=>0))->find();
            if($recharge_order_info){
                Db::name('recharge_order')->where(array('id'=>$recharge_order_info['id']))->update($add_order);
                $order_id=$recharge_order_info['id'];
            }else{
                $order_id=Db::name('recharge_order')->insertGetId($add_order);
            }
            Db::commit();
        }catch (Exception $e) {
            Db::rollback();
            return '';
        }
        $data=array(
            'order_sn'=>$add_order['order_sn'],
            'order_id'=>$order_id
        );
        return $data;
    }


    /**
     * 充值记录
     */
    public static function rechargeRecord($user_id,$get,$page,$size)
    {
       $list = Db::name('recharge_order')
        ->field('order_sn, order_amount, give_money, create_time')
        ->where([
          'user_id' => $user_id,
          'pay_status' => 1
        ])
        ->order('create_time', 'desc')
        ->page($page,$size)
        ->select();

        $count = Db::name('recharge_order')
              ->where([
                'user_id' => $user_id,
                'pay_status' => 1
              ])
              ->count();
        foreach($list as &$item) {
            $item['create_time'] = date('Y-m-d h:i:s', $item['create_time']);
            if($item['give_money'] > 0) {
              $item['desc'] = '充值'. $item['order_amount'] . '赠送' . $item['give_money'];
            }else{
              $item['desc'] = '充值'. $item['order_amount'];
            } 
            $item['total'] = $item['order_amount'] + $item['give_money']; // 充值金额 + 赠送金额
        }
        $more = is_more($count,$page,$size);  //是否有下一页
        $data = [
            'list'          => $list,
            'page_no'       => $page,
            'page_size'     => $size,
            'count'         => $count,
            'more'          => $more
        ];
        return $data;
    }
}